package com.amqr.testfragment.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.amqr.testfragment.R;
import com.amqr.testfragment.fragment.style1.FourFragment;
import com.amqr.testfragment.fragment.style1.OneFragment;
import com.amqr.testfragment.fragment.style1.ThreeFragment;
import com.amqr.testfragment.fragment.style1.TwoFragment;

public class StyleFirstActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup mRbTab;
    private RadioButton mRbOne;
    private RadioButton mRbTwo;
    private RadioButton mRbThree;
    private RadioButton mRbFour;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRbTab = (RadioGroup) findViewById(R.id.mRbTab);
        mRbOne = (RadioButton) findViewById(R.id.mRbOne);
        mRbTwo = (RadioButton) findViewById(R.id.mRbTwo);
        mRbThree = (RadioButton) findViewById(R.id.mRbThree);
        mRbFour = (RadioButton) findViewById(R.id.mRbFour);
        mRbTab.setOnCheckedChangeListener(this);
        commitOneFrag();
    }
    // MainActivty实现接口的必须复写的方法
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentManager fm = getSupportFragmentManager(); // getSupportFragmentManager(); 才是v4的
        FragmentTransaction transaction = fm.beginTransaction();  // Fragment的事务
        switch (checkedId) {
            case R.id.mRbOne:
                if (oneFragment == null) {
                    oneFragment = new OneFragment();
                }
                transaction.replace(R.id.mFlWaitReplace, oneFragment);
                break;
            case R.id.mRbTwo:
                if (twoFragment == null) {
                    twoFragment = new TwoFragment();
                }
                transaction.replace(R.id.mFlWaitReplace, twoFragment);
                break;
            case R.id.mRbThree:
                if (threeFragment == null) {
                    threeFragment = new ThreeFragment();
                }
                transaction.replace(R.id.mFlWaitReplace, threeFragment);
                break;
            case R.id.mRbFour:
                if (fourFragment == null) {
                    fourFragment = new FourFragment();
                }
                transaction.replace(R.id.mFlWaitReplace, fourFragment);
                break;
        }
        transaction.commit();
    }
    private void commitOneFrag() {
        OneFragment oneFrag = new OneFragment();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.mFlWaitReplace, oneFrag);
        ft.commit();
    }
}
